package {
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.text.*;
    import transforms.*;

    public class MagicBox extends Sprite {

        private var txt1:TextField;
        private var txt2:TextField;

        public function MagicBox() {
            trace(">> MagicBox Started!");
            
            //Create buttons
            var btn1:PlainButton = new PlainButton("UpperCase");
            var btn2:PlainButton = new PlainButton("LowerCase");
            var btn3:PlainButton = new PlainButton("Reverse");
            var btn4:PlainButton = new PlainButton("Split");
            var btn5:PlainButton = new PlainButton("2337");
            
            //Layout buttons
            btn1.x = 10; btn1.y = 30;
            btn2.x = 10; btn2.y = 70;
            btn3.x = 10; btn3.y = 110;
            btn4.x = 10; btn4.y = 150;
            btn5.x = 10; btn5.y = 190;
            
            //Add to stage
            addChild(btn1);
            addChild(btn2);
            addChild(btn3);
            addChild(btn4);
            addChild(btn5);
            
            //Add handlers
            btn1.addEventListener(MouseEvent.CLICK, clickHandler);
            btn2.addEventListener(MouseEvent.CLICK, clickHandler);
            btn3.addEventListener(MouseEvent.CLICK, clickHandler);
            btn4.addEventListener(MouseEvent.CLICK, clickHandler);
            btn5.addEventListener(MouseEvent.CLICK, clickHandler);
            
            //Create textfields
            txt1 = new TextField();
            txt2 = new TextField();
            
            //Layout textfields
            txt1.x = 160; txt1.y = 30; txt1.width = 200; txt1.height = 20;
            txt2.x = 160; txt2.y = 70; txt2.width = 200; txt2.height = 20;
            
            txt1.border = true; txt1.type = TextFieldType.INPUT;
            txt2.border = true; txt2.type = TextFieldType.INPUT;
            
            //Add to stage
            addChild(txt1);
            addChild(txt2);
        }
        
        private function clickHandler(e:MouseEvent):void
        {
            
            if(txt1.text == ""){
                txt2.text = "";
                trace("Nothing to convert");
                return;
            }
            
            var button:PlainButton = e.target as PlainButton;
            switch(button.label)
            {
                case "UpperCase":
                trace("UpperCase transform");
                var t0:UpperCaseTransform = new UpperCaseTransform();
                txt2.text = t0.run(txt1.text);
                break;
                case "LowerCase":
                trace("LowerCase transform");
                var t1:LowerCaseTransform = new LowerCaseTransform();
                txt2.text = t1.run(txt1.text);
                break;
                case "Reverse":
                trace("Reverse transform");
                var t2:ReverseTransform = new ReverseTransform();
                txt2.text = t2.run(txt1.text);
                break;
                case "Split":
                trace("Split transform");
                var t3:SplitTransform = new SplitTransform();
                txt2.text = t3.run(txt1.text);
                break;
                case "2337":
                trace("2337 transform");
                var t4:LeetTransform = new LeetTransform();
                txt2.text = t4.run(txt1.text);
                break;
                default:
                trace("Unkown button");
                txt2.text = "";
                break;
            }
        }
    }
}

